//
//  WHCategoryTableViewCell.m
//  GroupFourFM
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WHCategoryTableViewCell.h"
#import "WSFMData.h"
#import <BmobSDK/Bmob.h>
#import "WSBmob.h"

@implementation WHCategoryTableViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createCell];
    }
    
    return self;
}

-(void)createCell
{
    self.newsPic = [[UIImageView alloc] init];
    self.newsPic.backgroundColor = CLEAR;
    [self.contentView addSubview:self.newsPic];
    
    [self.newsPic mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.top.mas_equalTo(@10);
        make.bottom.mas_equalTo(@-10);
        make.width.equalTo(self.contentView.mas_height).offset(-20);
    }];
    
    
    self.newsTitle = [[WSBaseTitleLabel alloc] init];
    self.newsTitle.backgroundColor = CLEAR;
    self.newsTitle.font = [UIFont systemFontOfSize:19 weight:0.1];
    self.newsTitle.textColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    [self.contentView addSubview:self.newsTitle];
    [self.newsTitle mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.newsPic.mas_right).offset(10);
        make.top.equalTo(self.newsPic);
        make.right.mas_equalTo(@-40);
        make.height.equalTo(self.newsPic).multipliedBy(0.3);
    }];
    
    
    self.newsDigest = [[UILabel alloc] init];
    self.newsDigest.backgroundColor = CLEAR;
    self.newsDigest.font = [UIFont systemFontOfSize:15 weight:0];
    [self.contentView addSubview:self.newsDigest];
    self.newsDigest.textColor = [UIColor lightGrayColor];
    self.newsDigest.numberOfLines = 2;
    [self.newsDigest mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.newsTitle);
        make.top.equalTo(self.newsTitle.mas_bottom);
        make.right.mas_equalTo(@-40);
        make.height.equalTo(self.newsPic).multipliedBy(0.4);
    }];
    
    UIImageView *iconImageV = [[UIImageView alloc] init];
    iconImageV.backgroundColor = CLEAR;
    iconImageV.image = [UIImage imageNamed:@"sound_playtimes"];
    [self.contentView addSubview:iconImageV];
    [iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.newsDigest);
        make.centerY.equalTo(self.newsPic).multipliedBy(1.6);
        make.height.mas_equalTo(@10);
        make.width.mas_equalTo(@12);
    }];
    
    //播放人数
    self.contentCount = [[UILabel alloc] init];
    self.contentCount.backgroundColor = CLEAR;
    self.contentCount.font = [UIFont systemFontOfSize:13 weight:0];
    [self.contentView addSubview:self.contentCount];
    self.contentCount.textAlignment = NSTextAlignmentLeft;
    self.contentCount.textColor = [UIColor lightGrayColor];
    [self.contentCount mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(iconImageV.mas_right).offset(2);
        make.bottom.equalTo(self.newsPic);
        make.height.equalTo(self.newsPic).multipliedBy(0.3);
        make.width.mas_equalTo(@70);
    }];
    
    UIImageView *iconImage = [[UIImageView alloc] init];
    iconImage.backgroundColor = CLEAR;
    iconImage.image = [UIImage imageNamed:@"album_tracks"];
    [self.contentView addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.contentCount.mas_right);
        make.centerY.equalTo(self.contentCount);
        make.height.mas_equalTo(@15);
        make.width.mas_equalTo(@15);
    }];
    
    //集数
    self.duration = [[UILabel alloc] init];
    self.duration.backgroundColor = CLEAR;
    self.duration.font = [UIFont systemFontOfSize:13 weight:0];
    [self.contentView addSubview:self.duration];
    self.duration.textAlignment = NSTextAlignmentLeft;
    self.duration.textColor = [UIColor lightGrayColor];
    [self.duration mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(iconImage.mas_right).offset(2);
        make.bottom.equalTo(self.newsPic);
        make.height.equalTo(self.newsPic).multipliedBy(0.3);
        make.width.mas_equalTo(@70);
    }];
    
    //收藏按钮
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextBtn.backgroundColor = CLEAR;
    [self.contentView addSubview:self.nextBtn];
    
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.right.mas_equalTo(@-20);
        make.width.mas_equalTo(@30);
        make.height.mas_equalTo(@52.5);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self.contentView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.newsPic.mas_right).offset(0);
        make.top.equalTo(self.contentView.mas_bottom).offset(0);
        make.right.mas_equalTo(@0);
        make.height.mas_equalTo(@0.5);
        
    }];
    
}

-(void)setAlbumModel:(WSAlbumModel *)albumModel
{
    if (_albumModel != albumModel) {
        _albumModel = albumModel ;
    }
    
    NSURL *url = [NSURL URLWithString:albumModel.albumCoverUrl290];
    [self.newsPic sd_setImageWithURL:url placeholderImage:PLACEHOLDER_CELL_SAME];
    self.newsTitle.text = albumModel.title;
    self.newsDigest.text = albumModel.intro;
    self.contentCount.text = [NSString stringWithFormat:@"%.2f万", (CGFloat)albumModel.playsCounts/10000];
    [self.contentCount sizeToFit];
    self.duration.text = [NSString stringWithFormat:@"%@集", albumModel.tracksCounts];
    [self.duration sizeToFit];
    
    
    if (albumModel.isFavor) {
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_fav_n@2x.png"] forState:UIControlStateNormal];
        
    }else{
        
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_unfav_n@2x.png"] forState:UIControlStateNormal];
    }
    
    
    [self.nextBtn addTarget:self action:@selector(favorAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)favorAction:(UIButton *)btn
{
    if (_albumModel.isFavor) {
        //[[WSFMData shareFMDB] deleteByAlbumId:self.albumModel.myId];
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_unfav_n@2x.png"] forState:UIControlStateNormal];
        self.albumModel.isFavor = NO;

        BmobQuery *deleteAlbum = [BmobQuery queryWithClassName:@"Collect_album"];
        
        [deleteAlbum getObjectInBackgroundWithId:self.albumModel.objectId block:^(BmobObject *object, NSError *error) {
            
            if (error) {
                //进行错误处理
                NSLog(@"error -- %@",error);
            }
            else{
                if (object) {
                    //异步删除object
                    [object deleteInBackground];
                    NSLog(@"删除成功");
                }
            }
        }];
        
    }
    else{
        //[[WSFMData shareFMDB] insertAlbumData:self.albumModel];
        [self.nextBtn setImage:[UIImage imageNamed:@"find_album_fav_n@2x.png"] forState:UIControlStateNormal];
        self.albumModel.isFavor = YES;
        
        BmobObject *addAlbum = [BmobObject objectWithClassName:@"Collect_album"];
        
        
        NSDictionary *dic = @{@"albumName":self.albumModel.title,@"albumId":self.albumModel.myId,@"albumPic":self.albumModel.albumCoverUrl290,@"albumintro":self.albumModel.intro,@"albumtracks":self.albumModel.tracksCounts};
        [addAlbum saveAllWithDictionary:dic];
        
        [addAlbum saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            //进行操作
            if (isSuccessful) {
                //创建成功后的动作
                self.albumModel.objectId = addAlbum.objectId;
                NSLog(@"addAlbum.objectId -- %@",addAlbum.objectId);
                
                NSLog(@"添加成功");
            } else if (error){
                //发生错误后的动作
                NSLog(@"%@",error);
            } else {
                NSLog(@"Unknow error");
            }
        }];
        


    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
  
}


@end
